// pages/Businessmen/Businessmen.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';
import base64 from '../../utils/base64';

var loginFlag=false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    password: "",
    isRemember: true,
    istemplate: false,
    isGo: false,
    url: '',
    shopList: [{
        defaultImage: "https://yipeisong.oss-cn-hangzhou.aliyuncs.com/2/c/45dfa98b61e943a396f41df86de194e5.jpg",
        id: "1173801932867899392",
        image: '["https://yipeisong.oss-cn-hangzhou.aliyuncs.com/2/d/1d05cf4ca02f42ee9f30d821b9fe58c3.jpg","https://yipeisong.oss-cn-hangzhou.aliyuncs.com/2/e/8888b74be7ef46ac8357fe4815c7124d.jpg"]',
        introduction: "优质产地，科学种植，肥沃土壤，小玲珑可爱，甘甜可口，农场每日新鲜直供，只为爱小番",
        price: 10.8,
        saleCount: 0,
        shopId: "1173778416349089792",
        shopName: "鲜丰水果",
        storeCount: 888,
        title: "【圣女果】樱桃小番茄",
        type: "BUY",
        unit: "克、盒",
        weight: "500",
      },
      {
        defaultImage: "https://yipeisong.oss-cn-hangzhou.aliyuncs.com/2/c/12204614230846758e70f71bc23b3db0.jpg",
        id: "1173829069209473024",
        image: '["https://yipeisong.oss-cn-hangzhou.aliyuncs.com/2/d/70595b493fee4bf18fc4a414cbf82c39.jpg","https://yipeisong.oss-cn-hangzhou.aliyuncs.com/2/e/5e239da365e149b6ad6a809933ccc95b.jpg"]',
        introduction: "玉麒麟西瓜",
        price: 35.8,
        saleCount: 0,
        shopId: "1173778416349089792",
        shopName: "鲜丰水果",
        storeCount: 888,
        title: "玉麒麟西瓜",
        type: "BUY",
        unit: "斤",
        weight: "",
      },
      {
        defaultImage: "https://yipeisong.oss-cn-hangzhou.aliyuncs.com/e/4/a7b53f0e1ea340b88b83d440d265a27f.jpg",
        id: "1173787755075997696",
        image: '["https://yipeisong.oss-cn-hangzhou.aliyuncs.com/a/0/e1f41d7e86ec406eb54386f3da49380c.jpg","https://yipeisong.oss-cn-hangzhou.aliyuncs.com/a/f/b0de43a6c85a4ac19250a41416a5a2f4.jpg"]',
        introduction: "A级进口车厘子",
        price: 48.8,
        saleCount: 0,
        shopId: "1173778416349089792",
        shopName: "鲜丰水果",
        storeCount: 888,
        title: "A级进口车厘子",
        type: "BUY",
        unit: "克",
        weight: "250",
      },
      {
        defaultImage: "https://yipeisong.oss-cn-hangzhou.aliyuncs.com/2/d/c01e3b99b127444aa86c5039d7e2c164.jpg",
        id: "1173797863302500352",
        image: '["https://yipeisong.oss-cn-hangzhou.aliyuncs.com/2/e/dd2aead930b24e8aac4c39abc42db823.jpg","https://yipeisong.oss-cn-hangzhou.aliyuncs.com/3/0/d388a5eabca7466090e7b63da05933e0.jpg"]',
        introduction: "皮薄汁多味甜",
        price: 6.6,
        saleCount: 0,
        shopId: "1173778416349089792",
        shopName: "鲜丰水果",
        storeCount: 888,
        title: "云南阳光蜜桔子",
        type: "BUY",
        unit: "克",
        weight: "",
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('this is onload')
    var _this = this;
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log('this is onshow')
    let _this = this;
    _this.isProductRelease();
    if (wx.getStorageSync("userLogin")) {
      let userLogin = wx.getStorageSync("userLogin");
      _this.setData({
        name: userLogin.name,
        password: userLogin.password,
      })
    } else {
      _this.setData({
        name: "",
        password: "",
      })
    }
    var thirdSession = wx.getStorageSync('third_Session');
    if (thirdSession && !loginFlag) {
      console.log('==================')
      app.loadCartNum();
      loginFlag=true
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    app.globalData.otherPageHideFlag = true;
  },

  //登录按钮
  loginClick() {
    let _this = this;
    let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (_this.data.name == '' || _this.data.password == '') {
      console.log("信息不能为空");
      wx.showToast({
        title: '信息不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (!myreg.test(_this.data.name)) {
      console.log("手机号格式不正确");
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    //是否记住密码
    if (_this.data.isRemember) {
      let _this = this;
      let data = {
        name: _this.data.name,
        password: _this.data.password
      }
      wx.setStorageSync("userLogin", data)
    } else {
      wx.setStorageSync("userLogin", '')
    }
    let data = {
      'username': _this.data.name,
      'password': _this.data.password
    }
    //管理员登录
    Api.userLogin(data).then(res => {
      console.log("管理员登录", res);
      console.log(wx.getStorageSync("tokenObject"))
      //!wx.getStorageSync("tokenObject")
      if (!wx.getStorageSync("tokenObject")) {
        console.log("进入绑定");
        var redirect = app.globalData.Prefix + "/common/wx/bind/merchant";
        redirect = encodeURIComponent(redirect);
        console.log(redirect)
        var urll = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb550a18375e6e497&redirect_uri=" + redirect + "&response_type=code&scope=snsapi_base&state=" + res.data.id;
        console.log(urll)
        _this.setData({
          url: urll,
          isGo: true
        });
        setTimeout(function() {
          _this.setData({
            isGo: false
          });
        }, 1000);
      }
      //return;
      let Base64 = base64.Base64;
      let tokenObject = res.data.tokenObject;
      if (tokenObject) {
        let userMessage = JSON.parse(Base64.decode(tokenObject.access_token.split(".")[1]));
        tokenObject.access_tokenUser = userMessage;
        wx.setStorageSync("tokenObject", tokenObject);
        if (userMessage.roles.indexOf("merchant") != -1) { //是商户进入店铺
          wx.navigateTo({
            url: '/pages/template/template',
          })
        } else {
          wx.navigateTo({
            url: '/pages/template/orderList',
          })
        }
      }

    });

  },

  //获取用户名
  changeName(e) {
    let _this = this;
    _this.setData({
      name: e.detail.value,
    })
  },
  //获取密码
  changPassword(e) {
    let _this = this;
    _this.setData({
      password: e.detail.value,
    })
  },
  //记住密码
  rememberClick() {
    let _this = this;
    _this.setData({
      isRemember: !_this.data.isRemember
    })
  },


  //判断产品发布信息
  isProductRelease() {
    let _this = this;
    Api.isProductRelease().then(res => {
      console.log(res)
      _this.setData({
        istemplate: res.data
      })
    });
  },

})